//
//  HNSH_GroupMembersViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/17.
//

#import "HNSH_GroupMembersViewController.h"

//view
#import "HNSH_GroupMembersChangeCell.h"
#import "HNSH_GroupMembersCommonCell.h"

//vc
#import "HNSH_ChatFriendDetailViewController.h"     //好友信息
#import "HNSH_AddMembersViewController.h"           //添加群成员
#import "HNSH_DeleteMembersViewController.h"        //删除群成员

@interface HNSH_GroupMembersViewController ()
/** */
@property (nonatomic, copy)NSArray * membersArray;
@end

static NSString *HNSH_GroupMembersChangeCellID = @"HNSH_GroupMembersChangeCellID";
static NSString *HNSH_GroupMembersCommonCellID = @"HNSH_GroupMembersCommonCellID";

@implementation HNSH_GroupMembersViewController

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    [self configCollectionView];
            
    [self loadMembersData];
}

- (void)configCollectionView {
    [self.collectionView registerClass:[HNSH_GroupMembersChangeCell class] forCellWithReuseIdentifier:HNSH_GroupMembersChangeCellID];
    [self.collectionView registerClass:[HNSH_GroupMembersCommonCell class] forCellWithReuseIdentifier:HNSH_GroupMembersCommonCellID];
}

- (void)loadMembersData{
    [HNSH_ChatNetServer requestRongCloudGroupMembersWithGroupId:self.targetId success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            self.membersArray = [NSArray yy_modelArrayWithClass:HNSH_ChatFriendModel.class json:responseObject.resultset];
            [self showNavTitle:[NSString stringWithFormat:@"群成员(%ld)", self.membersArray.count] showLeftItem:YES];
        }
    } Failure:^(NSError * _Nullable error) {
            
    }];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.view addSubview:self.collectionView];
}

//MARK: Setter Methods
- (void)setMembersArray:(NSArray *)membersArray{
    _membersArray = membersArray;
    
    [self.collectionView reloadData];
}

//MARK: UICollectionViewDelegate Methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.role) {
        return self.membersArray.count + 2;
    } else {
        return self.membersArray.count + 1;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row > self.membersArray.count) {
        HNSH_GroupMembersChangeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_GroupMembersChangeCellID forIndexPath:indexPath];
        cell.isDelete = YES;
        return cell;
    } else if (indexPath.row == self.membersArray.count) {
        HNSH_GroupMembersChangeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_GroupMembersChangeCellID forIndexPath:indexPath];
        cell.isDelete = NO;
        return cell;
    } else {
        HNSH_GroupMembersCommonCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_GroupMembersCommonCellID forIndexPath:indexPath];
        cell.model = self.membersArray[indexPath.row];
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (kScreenWidth - 24 - 40) / 5;
    return CGSizeMake(width, 10 + width + 5 + 17 + 10);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 12, 0, 12);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row > self.membersArray.count) {
        //删除
        HNSH_DeleteMembersViewController * vc = [HNSH_DeleteMembersViewController new];
        vc.targetId = self.targetId;
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.row == self.membersArray.count) {
        //添加
        HNSH_AddMembersViewController * vc = [HNSH_AddMembersViewController new];
        vc.targetId = self.targetId;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        HNSH_ChatFriendModel * model = self.membersArray[indexPath.row];
        HNSH_ChatFriendDetailViewController * vc = [HNSH_ChatFriendDetailViewController new];
        vc.rcUserId = model.user.uid;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
